﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///Adv 的摘要说明
/// </summary>
public partial class Adv
{

    public enum SYplace { 热门导航上 = 1, 热门导航下 = 2 }
    public enum LBplace { 广告位置1 = 4, 广告位置2 = 5 }
    public enum RMDHType { 家教 = 11, 机构, 老师, 培训 }
    public enum GRZX { 常用入口 = 15 }
    public static Adv Get(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Adv.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<Adv> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Adv.ToList();
        }
    }
    public static List<Adv> GetAllByConditon(int AdvPlace, string Key)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from a in context.Adv
                        select a;
            if (AdvPlace != 0)
                query = query.Where(row => row.AdvPlace == AdvPlace);

            if (!string.IsNullOrEmpty(Key))
                query = query.Where(row => row.Link.Contains(Key) || row.Title.Contains(Key));
            return query.ToList();
        }
    }
    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.Adv.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<Adv>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.Adv.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.Adv.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

}